package com.zxjbyte.yiyi.module.system.service.log.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zxjbyte.yiyi.framework.common.domain.api.PageResult;
import com.zxjbyte.yiyi.framework.common.domain.api.ResultCode;
import com.zxjbyte.yiyi.framework.common.exception.ServiceException;
import com.zxjbyte.yiyi.framework.common.util.AuthUtil;
import com.zxjbyte.yiyi.framework.mybatis.service.impl.BaseServiceXImpl;
import com.zxjbyte.yiyi.framework.mybatis.util.Condition;
import com.zxjbyte.yiyi.module.system.convert.log.ErrorLogConvert;
import com.zxjbyte.yiyi.module.system.domain.dto.log.ErrorLogAddDTO;
import com.zxjbyte.yiyi.module.system.domain.entity.ErrorLogPO;
import com.zxjbyte.yiyi.module.system.domain.request.log.ErrorLogPageRequest;
import com.zxjbyte.yiyi.module.system.domain.request.log.ErrorLogProcessRequest;
import com.zxjbyte.yiyi.module.system.domain.response.log.ErrorLogPageResponse;
import com.zxjbyte.yiyi.module.system.mapper.log.ErrorLogMapper;
import com.zxjbyte.yiyi.module.system.service.log.IErrorLogService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.zxjbyte.yiyi.framework.common.enums.SortOrder.DESC;


/**
 * @Author zhangxingjia
 * @Date 2023/3/17 17:31
 * @Version: 1.0
 */
@Service
public class ErrorLogServiceImpl extends BaseServiceXImpl<ErrorLogMapper, ErrorLogPO> implements IErrorLogService {

    @Override
    public PageResult<ErrorLogPageResponse> page(ErrorLogPageRequest pageRequest) {
        LambdaQueryWrapper<ErrorLogPO> wrapper = Wrappers.<ErrorLogPO>lambdaQuery().ge(Objects.nonNull(pageRequest.getStartExTime()), ErrorLogPO::getExceptionTime, pageRequest.getStartExTime())
                .le(Objects.nonNull(pageRequest.getEndExTime()), ErrorLogPO::getExceptionTime, pageRequest.getEndExTime())
                .eq(Objects.nonNull(pageRequest.getProcessStatus()), ErrorLogPO::getProcessStatus, pageRequest.getProcessStatus());
        IPage<ErrorLogPO> page = page(Condition.getPage(pageRequest, ErrorLogPO::getCreateTime, DESC), wrapper);
        List<ErrorLogPageResponse> responseList = ErrorLogConvert.INSTANCE.pageConvert(page.getRecords());
        return PageResult.of(responseList, page);
    }

    @Override
    public void process(ErrorLogProcessRequest request) {
        if(request.getId() <= 0 || Objects.isNull(getById(request.getId()))){
            throw new ServiceException(ResultCode.PARAM_VALID_ERROR);
        }
        lambdaUpdate().set(ErrorLogPO::getProcessStatus, request.getProcessStatus())
                .set(ErrorLogPO::getProcessTime, new Date())
                .set(ErrorLogPO::getProcessUserId, AuthUtil.getUserId())
                .set(ErrorLogPO::getProcessRemarks, request.getProcessRemarks())
                .eq(ErrorLogPO::getId, request.getId()).update();
    }

    @Override
    public void add(ErrorLogAddDTO addReqDto) {
        ErrorLogPO errorLogPo = ErrorLogConvert.INSTANCE.convert(addReqDto);
        errorLogPo.setRequestParams(StrUtil.sub(errorLogPo.getRequestParams(), 0, ErrorLogPO.REQUEST_PARAMS_MAX_LENGTH));
        save(errorLogPo);
    }
}
